package com.hnkywl.sts.dao.sfgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.jhgl.Jkd;
import com.hnkywl.sts.entity.sfgl.Sfkmx;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.sfgl
 * 类描述:    付款审核dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Repository
public class FkshmxDao extends SimpleHibernateTemplate<Sfkmx, Long> {

    public Page<Sfkmx> findFkshmxList(Long fkshId,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Sfkmx> page = new Page<Sfkmx>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (fkshId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" fksh.id = ? ");
            param.add(fkshId);
        } else {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" fksh.id is null ");
        }
        //==============记录数量===============
        hql = "select count(*) from Sfkmx where 1=1 "+ RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Sfkmx where 1=1"+ RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and  " + condition;
            hql = hql + " order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public Page<Sfkmx> findAllFkshmxByJkd(Long jkdId,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Sfkmx> page = new Page<Sfkmx>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (jkdId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jkd.id = ? ");
            param.add(jkdId);
        }
        //==============记录数量===============
        hql = "select count(*) from Sfkmx ";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Sfkmx ";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        hql = hql + " order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }


    /**
     * 根据收付款ID查询电子借款单
     * @param fkshId
     * @return
     */
    public List<Sfkmx> findBySfkmxId(Long fkshId){
        return find("from Sfkmx where fksh.id = ?",fkshId);
    }

    /**
     * 根据收付款ID查询电子借款单
     * @param
     * @return
     */
    public List<Sfkmx> findBySfkmxByJkdId(Long jkdId){
        return find("from Sfkmx where jkd.id = ? ",jkdId);
    }
}
